﻿using System;
using System.Drawing.Text;
using System.Text;
using System.Windows;
using System.Windows.Input;
using MyOS.Utils;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;

namespace MyOS.Views.ChildView
{
    /// <summary>
    ///     NewFileView.xaml 的交互逻辑
    /// </summary>
    public partial class NewFileView : WindowX
    {
        private readonly char diskName;
        private readonly InstalledFontCollection objFont;
        public bool isSuccess;
        private string path; //文件来源


        public NewFileView(string path, char diskName)
        {
            InitializeComponent();
            this.path = path;
            this.diskName = diskName;
            objFont = new InstalledFontCollection();
            FontStyle.Click += changeFontSize;
            foreach (var font in objFont.Families) FontStyle.Items.Add(font.Name);
        }

        public string fileName { get; set; }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void BtnMin_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void BtnMax_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }

        private void WindowX_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(TextEditor);
        }

        private void Button保存_Click(object sender, RoutedEventArgs e)
        {
            var textBytes = Encoding.Default.GetBytes(TextEditor.Text);
            //将文件内容转换为数组
            var fileLength = (short) textBytes.Length;
            //先看看大小够不够
            var startBlock = DiskUtils.findEmptyBlock(diskName, fileLength);
            if (textBytes.Length == 0)
            {
                var result = MessageBoxX.Show("文件不能为空！", "Error", Application.Current.MainWindow,
                    MessageBoxButton.OK, new MessageBoxXConfigurations
                    {
                        OKButton = "好的",
                        MinHeight = 180,
                        MinWidth = 300,
                        MessageBoxIcon = MessageBoxIcon.Error,
                        ButtonBrush = "#FF4C4C".ToColor().ToBrush()
                    });
            }
            else if (startBlock == -1)
            {
                var result = MessageBoxX.Show("磁盘空间不足，保存失败", "Error", Application.Current.MainWindow,
                    MessageBoxButton.OK, new MessageBoxXConfigurations
                    {
                        OKButton = "好的",
                        MinHeight = 180,
                        MinWidth = 300,
                        MessageBoxIcon = MessageBoxIcon.Error,
                        ButtonBrush = "#FF4C4C".ToColor().ToBrush()
                    });
            }
            else
            {
                var window = new RenameView();
                window.getFileName += SetFileName;

                IsMaskVisible = true;
                ((MainWindow) Application.Current.MainWindow).IsMaskVisible = true;
                window.ShowDialog();
                IsMaskVisible = false;

                if (fileName != null)
                {
                    // ((NewFileView)Application.Current.MainWindow).IsMaskVisible = false;
                    if (DiskUtils.searchIsExists(diskName, fileName, 'e') != -1)
                    {
                        MessageBoxX.Show("同名文件已存在！", "Error", Application.Current.MainWindow,
                            MessageBoxButton.OK, new MessageBoxXConfigurations
                            {
                                OKButton = "好的",
                                MinHeight = 180,
                                MinWidth = 300,
                                MessageBoxIcon = MessageBoxIcon.Error,
                                ButtonBrush = "#FF4C4C".ToColor().ToBrush()
                            });
                    }
                    else
                    {
                        var attrBytes = new byte[20];
                        var fileNameBytes = Encoding.Default.GetBytes(fileName); //前三个字节写入文件名
                        for (var i = 0; i < fileNameBytes.Length; i++) attrBytes[i] = fileNameBytes[i];

                        //文件属性
                        var extensionName = 'e'; //扩展名
                        attrBytes[3] = Convert.ToByte(extensionName);
                        var isRead = this.isRead.IsChecked ? 1 : 0; //文件属性，可读为1
                        var isWrite = this.isWrite.IsChecked ? 2 : 0; //文件属性，可写为2
                        attrBytes[4] = Convert.ToByte(isRead + isWrite); //可读可写为3,都没有为0
                        attrBytes[5] = Convert.ToByte(startBlock); //起始盘块号
                        attrBytes[6] = BitConverter.GetBytes(fileLength)[0]; //文件长度
                        attrBytes[7] = BitConverter.GetBytes(fileLength)[1]; //文件长度
                        attrBytes[8] = Convert.ToByte(DateTime.Now.ToString("yy")); //创建时间
                        attrBytes[9] = Convert.ToByte(DateTime.Now.Month);
                        attrBytes[10] = Convert.ToByte(DateTime.Now.Day);
                        attrBytes[11] = Convert.ToByte(DateTime.Now.Hour);
                        attrBytes[12] = Convert.ToByte(DateTime.Now.Minute);
                        attrBytes[13] = Convert.ToByte(DateTime.Now.Second);
                        DiskUtils.writeToRoot(diskName, attrBytes);
                        DiskUtils.writeToFatAndDisk(diskName, textBytes, startBlock);
                        Notice.Show("保存成功！", "Success", 3, MessageBoxIcon.Success);
                        isSuccess = true;
                        Close();
                    }
                }
            }
        }

        private void Button清空_Click(object sender, RoutedEventArgs e)
        {
        }

        public void SetFileName(string value)
        {
            fileName = value;
        }

        private void changeFontSize(object sender, RoutedEventArgs e)
        {
        }
    }
}